﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Linq;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using SMS.Entities.BaseClasses;
using SMS.Common.Silverlight;
using SMS.Model;
using SMS.Entities;
using System.Windows.Data;
using SMS.Entities.Report;

namespace SMS.ViewModel.Reports
{
    public class SalesHistoryReportViewModel : BaseReport
    {
        ReportModel _model;
        public SalesHistoryReportViewModel()
        {
            SearchCommand = new DelegateCommand(Search);
            _model = new ReportModel();
            _model.SaleDetailReportDataCompleted += new Action<ObservableCollection<Entities.Report.SaleReport>>(_model_SaleDetailReportDataCompleted);
            _model.OnServiceError += new Action<SMSException>(_model_OnServiceError);
        }
        public event Action<string> OnReportChanged;
        void _model_SaleDetailReportDataCompleted(ObservableCollection<Entities.Report.SaleReport> obj)
        {
            if (obj != null)
            {
                PagedCollectionView paged = new PagedCollectionView(obj);
                ReportCollection = paged;
                if (OnReportChanged != null)
                    OnReportChanged(this.SelectedSearchOption);
                UserMessage = "Report Data Loaded Successfully...";
            }
        }

        void _model_OnServiceError(SMSException obj)
        {
            ErrorMessage = obj.Message;
            UserMessage = string.Empty;
        }

        public ICommand SearchCommand { get; set; }
        private void Search(object param)
        {
            UserMessage = "Report Data Loading...";
            _model.GetSaleDetailReportData(App.LoggedinUserName, FromDate, EndDate, SelectedSearchOption == null ? "" : SelectedSearchOption);
        }

        private SaleReport _SelectedSaleReportFilterData;

        public SaleReport SelectedSaleReportFilterData
        {
            get { return _SelectedSaleReportFilterData; }
            set { _SelectedSaleReportFilterData = value; OnPropertyChanged("SelectedSaleReportFilterData"); }
        }

        private PagedCollectionView _ReportCollection;

        public PagedCollectionView ReportCollection
        {
            get { return _ReportCollection; }
            set { _ReportCollection = value; OnPropertyChanged("ReportCollection"); }
        }


    }
}
